package com.mall.portal.service.Impl;

import com.mall.common.pojo.TaotaoResult;
import com.mall.common.utils.HttpClientUtil;
import com.mall.common.utils.JsonUtils;
import com.mall.pojo.TbContent;
import com.mall.portal.service.ContentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ContentServiceImpl implements ContentService{

    @Value("${REST_BASE_URL}")
    private String REST_BASE_URL;

    @Value("${REST_INDEX_AD_URL}")
    private String REST_INDEX_AD_URL;

    @Override
    public String getContentList() {
        String result=HttpClientUtil.doGet(REST_BASE_URL+REST_INDEX_AD_URL);

        try {
            TaotaoResult taotaoResult=TaotaoResult.formatToList(result, TbContent.class);
            List<TbContent> list=(List<TbContent>) taotaoResult.getData();

            List<Map> resultList=new ArrayList<>();

            for (TbContent t:list) {
                Map map=new HashMap<>();
                map.put("src", t.getPic());
                map.put("height", 240);
                map.put("width", 670);
                map.put("srcB", t.getPic2());
                map.put("widthB", 550);
                map.put("heightB", 240);
                map.put("href", t.getUrl());
                map.put("alt", t.getSubTitle());
                resultList.add(map);
            }

            return JsonUtils.objectToJson(resultList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
